<?php
namespace app\admin\controller;

use app\admin\model\Links as LinksModel;
use app\admin\controller\Base;

class Links extends Base


{
    public function lit()
    {
        $list = LinksModel::paginate(3);
        $this->assign('list', $list);
        return $this->fetch('lit');
    }
    public function add()
    {
        if (request()->isPost()) {
            $data['title'] = input('title');
            $data['url'] = input('url');
            $data['desc'] = input('desc');
            $result = db('links')->insert($data);
            if ($result) {
                $this->success('友情链接添加成功', 'lit');
            } else {
                $this->error('友情链接添加失败');
            }
        }
        return $this->fetch('add');
    }
    public function edit()
    {
        $id = input('id');
        $admins = db('links')->where('id', $id)->find();
        if (request()->isPost()) {
            $data['title'] = input('title');
            $data['url'] = input('url');
            $data['desc'] = input('desc');
            $result = db('links')->where('id', $id)->update($data);
            if ($result) {
                $this->success('更改成功', 'lit');
            } else {
                $this->error('更改不成功');
            }

        } else {
            $this->assign('admins', $admins);
            return $this->fetch('edit');
        }

    }
    public function del()
    {
        $result = db('links')->where('id', input('id'))->delete();
        if ($result) {
            $this->success('删除成功', 'lit');
        } else {
            $this->success('删除失败', 'lit');
        }
    }
}
